
package com.android.fitnesscenter.view;

import java.util.Calendar;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.android.fitnesscenter.R;
import com.android.fitnesscenter.utils.ConfigProperties;
import com.android.fitnesscenter.utils.ConfigProperties.RoleType;
import com.android.fitnesscenter.utils.Loger;
import com.android.fitnesscenter.utils.SharePrefsHelper;
import com.android.fitnesscenter.utils.ViewHelper;
import com.android.fitnesscenter.view.client.ClientLoginActivity;
import com.android.fitnesscenter.view.server.ServerLoginActivity;
import com.android.fitnesscenter.view.server.ServerNewUserActivity;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

public class MainSelectActivity extends BaseActivity {

    @ViewInject(R.id.btn_user)
    public Button btn_user;

    @ViewInject(R.id.btn_manager)
    public Button btn_manager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_select_layout);
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        Log.e("MainActivity", "year is " + year + " month is " + month + " day is " + day);
        if (month >= 11 && day > 5) {
            Toast.makeText(MainSelectActivity.this, "试用期已结束", Toast.LENGTH_SHORT).show();
            finish();
        }
        initTitleBar("请选择你的角色");
        ibtn_back.setVisibility(View.GONE);
        ViewUtils.inject(this);

    }

    @OnClick(R.id.btn_user)
    public void btn_user(View view) {
        Intent intent = new Intent(MainSelectActivity.this, ClientLoginActivity.class);
        startActivity(intent);
    }

    @OnClick(R.id.btn_manager)
    public void btn_manager(View view) {
        SharePrefsHelper helper = new SharePrefsHelper(ConfigProperties.SHARE_PREFS);
        boolean isFirstLogin = helper.getBoolean("is_first_login", true);
        if (isFirstLogin) {
            queryTypeExs();
        } else {
            Intent intent = new Intent(MainSelectActivity.this, ServerLoginActivity.class);
            startActivity(intent);
        }
    }

    private ProgressDialog mDialog;

    private void queryTypeExs() {
        RequestParams params = new RequestParams();
        HttpUtils http = new HttpUtils();
        params.addBodyParameter("type", RoleType.MANAGER);

        String url = ConfigProperties.SERVICE_URL + "queryTypeExs.do";
        Loger.e("url is " + url + " params is " + params);
        http.send(HttpRequest.HttpMethod.POST, url, params, new RequestCallBack<String>() {
            @Override
            public void onStart() {
                mDialog = new ProgressDialog(MainSelectActivity.this);
                mDialog.setCanceledOnTouchOutside(false);
                mDialog.setMessage("加载中,请等待...");
                mDialog.show();
            }

            @Override
            public void onSuccess(ResponseInfo<String> stringResponseInfo) {
                if (MainSelectActivity.this != null && !MainSelectActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    if (stringResponseInfo != null && !TextUtils.isEmpty(stringResponseInfo.result)) {
                        Loger.e("stringResponseInfo.result is " + stringResponseInfo.result);
                        try {
                            JSONObject jsonObject = new JSONObject(stringResponseInfo.result);
                            if (jsonObject != null) {
                                boolean success = jsonObject.getBoolean("success");
                                if (success) {
                                    if (jsonObject.has("list")) {
                                        JSONArray listArray = jsonObject.getJSONArray("list");
                                        if (listArray != null && listArray.length() > 0) {
                                            Intent intent = new Intent(MainSelectActivity.this,
                                                    ServerLoginActivity.class);
                                            startActivity(intent);
                                            SharePrefsHelper helper = new SharePrefsHelper(ConfigProperties.SHARE_PREFS);
                                            helper.setBoolean("is_first_login", false);
                                        } else {
                                            Intent intent = new Intent(MainSelectActivity.this,
                                                    ServerNewUserActivity.class);
                                            intent.putExtra("role_type", RoleType.MANAGER);
                                            startActivity(intent);
                                        }
                                    } else {
                                        Intent intent = new Intent(MainSelectActivity.this, ServerNewUserActivity.class);
                                        intent.putExtra("role_type", RoleType.MANAGER);
                                        startActivity(intent);
                                    }
                                } else {
                                    String msg = jsonObject.getString("message");
                                    new ViewHelper(MainSelectActivity.this).showBTNDialog(msg);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    } else {
                        new ViewHelper(MainSelectActivity.this).showBTNDialog("加载失败,请重试!");
                    }
                }
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                Loger.e("msg:" + msg);
                if (MainSelectActivity.this != null && !MainSelectActivity.this.isFinishing()) {
                    if (mDialog != null) {
                        mDialog.dismiss();
                    }
                    new ViewHelper(MainSelectActivity.this).showErrorDialog(error);
                }
            }
        });

    }

    @Override
    protected void titlebarMethod(int id) {

    }
}
